package team.unstudio.unknownrpg_base.attribute;

import java.util.HashMap;
import java.util.Map;

import indi.defoliation.unknownrpg.api.attribute.Attribute;
import indi.defoliation.unknownrpg.api.attribute.AttributeBody;
import indi.defoliation.unknownrpg.api.attribute.AttributeEventHandler;
import indi.defoliation.unknownrpg.api.attribute.AttributePriority;
import indi.defoliation.unknownrpg.api.attribute.NumberAttribute;
import indi.defoliation.unknownrpg.api.rpgevent.entity.EntityAttackEvent;

import static team.unstudio.unknownrpg_base.Util.lucky;

public class BlockChanceAttribute extends Attribute implements NumberAttribute<Integer>{
	
	private int chance;
	
	public BlockChanceAttribute(int chance) {
		this.set(chance);
	}
	
	@Override
	public Map<String, Object> serialize() {
		HashMap<String,Object> map = new HashMap<>();
		map.put("chance", chance);
		return map;
	}

	@Override
	public void add(Integer t) {
		chance+=t;
		if(chance>100)
			chance=100;
	}

	@Override
	public Integer get() {
		return chance;
	}

	@Override
	public void set(Integer t) {
		this.chance=t;
	}

	@Override
	public String getKeyName() {
		return "BlockChance";
	}

	@Override
	public String getDisplayName() {
		return "BlockChance";
	}

	@Override
	public void deserialize(Map<String, Object> map) {
		this.chance=(int) map.get("chance");
	}
	
	@Override
	public Attribute getAttribute() {
		return this;
	}
	
	@AttributeEventHandler(priority=AttributePriority.HIGH,body = {AttributeBody.MAIN_HAND,AttributeBody.OFF_HAND})
	public void onAttack(EntityAttackEvent event,Map<AttributeBody,Attribute> map) {
		if(lucky(((BlockChanceAttribute)map.get(AttributeBody.MAIN_HAND)).get())&&
				lucky(((BlockChanceAttribute)map.get(AttributeBody.OFF_HAND)).get())) {
			event.putLetter(this, true);
		}
	}
}
